<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>MongoDB Update | Introduction</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../../home/part2/5.MongoDBDelete.html" />
    
    
    <link rel="prev" href="../../home/part2/3.MongoDBCreate.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="3.4"
        data-chapter-title="MongoDB Update"
        data-filepath="home/part2/4.MongoDBUpdate.md"
        data-basepath="../.."
        data-revision="Sun Oct 21 2018 20:49:12 GMT+0800 (中国标准时间)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        Introduction
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="home/NoSQL简介.html">
            
                
                    <a href="../../home/NoSQL简介.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        NoSQL介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2" data-path="home/part1/Redis.html">
            
                
                    <a href="../../home/part1/Redis.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        Redis
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="home/part1/1.常用命令.html">
            
                
                    <a href="../../home/part1/1.常用命令.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        常用命令
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="home/part1/2.Redis的数据类型.html">
            
                
                    <a href="../../home/part1/2.Redis的数据类型.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        Redis的数据类型
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="home/part1/3.Redis高级.html">
            
                
                    <a href="../../home/part1/3.Redis高级.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        Redis高级
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="home/part1/4.python操作redis.html">
            
                
                    <a href="../../home/part1/4.python操作redis.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        python操作redis
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="home/part2/MongoDB.html">
            
                
                    <a href="../../home/part2/MongoDB.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        MongoDB
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="home/part2/1.MongoDB基础操作.html">
            
                
                    <a href="../../home/part2/1.MongoDB基础操作.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        MongoDB基础操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="home/part2/2.MongoDBDBS.html">
            
                
                    <a href="../../home/part2/2.MongoDBDBS.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        MongoDB DBS
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="home/part2/3.MongoDBCreate.html">
            
                
                    <a href="../../home/part2/3.MongoDBCreate.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        MongoDB Create
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="3.4" data-path="home/part2/4.MongoDBUpdate.html">
            
                
                    <a href="../../home/part2/4.MongoDBUpdate.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        MongoDB Update
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="home/part2/5.MongoDBDelete.html">
            
                
                    <a href="../../home/part2/5.MongoDBDelete.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                        MongoDB Delete
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="home/part2/6.MongoDBRetrieve.html">
            
                
                    <a href="../../home/part2/6.MongoDBRetrieve.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.6.</b>
                        
                        MongoDB Retrieve
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.7" data-path="home/part2/7.MongoDB查询条件.html">
            
                
                    <a href="../../home/part2/7.MongoDB查询条件.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.7.</b>
                        
                        MongoDB 查询条件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.8" data-path="home/part2/8.MongoDBIndex.html">
            
                
                    <a href="../../home/part2/8.MongoDBIndex.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.8.</b>
                        
                        MongoDB Index
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.9" data-path="home/part2/9.MongoDBDump.html">
            
                
                    <a href="../../home/part2/9.MongoDBDump.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.9.</b>
                        
                        MongoDB Dump
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.10" data-path="home/part2/10.Python操作MongoDB.html">
            
                
                    <a href="../../home/part2/10.Python操作MongoDB.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.10.</b>
                        
                        Python操作MongoDB
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../../" >Introduction</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="mongodb-&#x66F4;&#x65B0;&#x6587;&#x6863;">MongoDB &#x66F4;&#x65B0;&#x6587;&#x6863;</h1>
<p>MongoDB &#x4F7F;&#x7528;update()&#x548C;save()&#x65B9;&#x6CD5;&#x6765;&#x66F4;&#x65B0;&#x96C6;&#x5408;&#x4E2D;&#x7684;&#x6587;&#x6863;&#x3002;&#x63A5;&#x4E0B;&#x6765;&#x8BA9;&#x6211;&#x4EEC;&#x8BE6;&#x7EC6;&#x6765;&#x770B;&#x4E0B;&#x4E24;&#x4E2A;&#x51FD;&#x6570;&#x7684;&#x5E94;&#x7528;&#x53CA;&#x5176;&#x533A;&#x522B;&#x3002;</p>
<hr>
<h3 id="update-&#x65B9;&#x6CD5;">update() &#x65B9;&#x6CD5;</h3>
<p>update() &#x65B9;&#x6CD5;&#x7528;&#x4E8E;&#x66F4;&#x65B0;&#x5DF2;&#x5B58;&#x5728;&#x7684;&#x6587;&#x6863;&#x3002;&#x8BED;&#x6CD5;&#x683C;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-sql">db.collection.update(
    &lt;query&gt;,
    &lt;update&gt;,
    {
        upsert: &lt;boolean&gt;,
        multi: &lt;boolean&gt;,
        writeConcern: &lt;document&gt;
    }
)
</code></pre>
<p>&#x53C2;&#x6570;&#x8BF4;&#x660E;&#xFF1A;</p>
<ul>
<li><strong>query</strong> : update&#x7684;&#x67E5;&#x8BE2;&#x6761;&#x4EF6;&#xFF0C;&#x7C7B;&#x4F3C;sql update&#x67E5;&#x8BE2;&#x5185;where&#x540E;&#x9762;&#x7684;&#x3002;</li>
<li><strong>update</strong> : update&#x7684;&#x5BF9;&#x8C61;&#x548C;&#x4E00;&#x4E9B;&#x66F4;&#x65B0;&#x7684;&#x64CD;&#x4F5C;&#x7B26;&#xFF08;&#x5982;$,$inc...&#xFF09;&#x7B49;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x7406;&#x89E3;&#x4E3A;sql update&#x67E5;&#x8BE2;&#x5185;set&#x540E;&#x9762;&#x7684;</li>
<li><strong>upsert</strong> : &#x53EF;&#x9009;&#xFF0C;&#x8FD9;&#x4E2A;&#x53C2;&#x6570;&#x7684;&#x610F;&#x601D;&#x662F;&#xFF0C;&#x5982;&#x679C;&#x4E0D;&#x5B58;&#x5728;update&#x7684;&#x8BB0;&#x5F55;&#xFF0C;&#x662F;&#x5426;&#x63D2;&#x5165;objNew,true&#x4E3A;&#x63D2;&#x5165;&#xFF0C;&#x9ED8;&#x8BA4;&#x662F;false&#xFF0C;&#x4E0D;&#x63D2;&#x5165;&#x3002;</li>
<li><strong>multi</strong> : &#x53EF;&#x9009;&#xFF0C;mongodb &#x9ED8;&#x8BA4;&#x662F;false,&#x53EA;&#x66F4;&#x65B0;&#x627E;&#x5230;&#x7684;&#x7B2C;&#x4E00;&#x6761;&#x8BB0;&#x5F55;&#xFF0C;&#x5982;&#x679C;&#x8FD9;&#x4E2A;&#x53C2;&#x6570;&#x4E3A;true,&#x5C31;&#x628A;&#x6309;&#x6761;&#x4EF6;&#x67E5;&#x51FA;&#x6765;&#x591A;&#x6761;&#x8BB0;&#x5F55;&#x5168;&#x90E8;&#x66F4;&#x65B0;&#x3002;</li>
<li><strong>writeConcern</strong> :&#x53EF;&#x9009;&#xFF0C;&#x629B;&#x51FA;&#x5F02;&#x5E38;&#x7684;&#x7EA7;&#x522B;&#x3002;</li>
</ul>
<h3 id="&#x5B9E;&#x4F8B;">&#x5B9E;&#x4F8B;</h3>
<p>&#x6211;&#x4EEC;&#x5728;&#x96C6;&#x5408; col &#x4E2D;&#x63D2;&#x5165;&#x5982;&#x4E0B;&#x6570;&#x636E;&#xFF1A;</p>
<pre><code class="lang-sql">&gt;db.col.insert({
    title: &apos;MongoDB &#x6559;&#x7A0B;&apos;, 
    description: &apos;MongoDB &#x662F;&#x4E00;&#x4E2A; Nosql &#x6570;&#x636E;&#x5E93;&apos;,
    by: &apos;&#x83DC;&#x9E1F;&#x6559;&#x7A0B;&apos;,
    url: &apos;http://www.runoob.com&apos;,
    tags: [&apos;mongodb&apos;, &apos;database&apos;, &apos;NoSQL&apos;],
    likes: 100
})
</code></pre>
<p>&#x63A5;&#x7740;&#x6211;&#x4EEC;&#x901A;&#x8FC7; update() &#x65B9;&#x6CD5;&#x6765;&#x66F4;&#x65B0;&#x6807;&#x9898;(title):</p>
<pre><code class="lang-sql">&gt;db.col.update({&apos;title&apos;:&apos;MongoDB &#x6559;&#x7A0B;&apos;},{$set:{&apos;title&apos;:&apos;MongoDB&apos;}})
WriteResult({ &quot;nMatched&quot; : 1, &quot;nUpserted&quot; : 0, &quot;nModified&quot; : 1 })   # &#x8F93;&#x51FA;&#x4FE1;&#x606F;
&gt; db.col.find().pretty()
{
    &quot;_id&quot; : ObjectId(&quot;56064f89ade2f21f36b03136&quot;),
    &quot;title&quot; : &quot;MongoDB&quot;,
    &quot;description&quot; : &quot;MongoDB &#x662F;&#x4E00;&#x4E2A; Nosql &#x6570;&#x636E;&#x5E93;&quot;,
    &quot;by&quot; : &quot;&#x83DC;&#x9E1F;&#x6559;&#x7A0B;&quot;,
    &quot;url&quot; : &quot;http://www.runoob.com&quot;,
    &quot;tags&quot; : [
            &quot;mongodb&quot;,
            &quot;database&quot;,
            &quot;NoSQL&quot;
    ],
    &quot;likes&quot; : 100
}
&gt;
</code></pre>
<p>&#x53EF;&#x4EE5;&#x770B;&#x5230;&#x6807;&#x9898;(title)&#x7531;&#x539F;&#x6765;&#x7684; &quot;MongoDB &#x6559;&#x7A0B;&quot; &#x66F4;&#x65B0;&#x4E3A;&#x4E86; &quot;MongoDB&quot;&#x3002;</p>
<p>&#x4EE5;&#x4E0A;&#x8BED;&#x53E5;&#x53EA;&#x4F1A;&#x4FEE;&#x6539;&#x7B2C;&#x4E00;&#x6761;&#x53D1;&#x73B0;&#x7684;&#x6587;&#x6863;&#xFF0C;&#x5982;&#x679C;&#x4F60;&#x8981;&#x4FEE;&#x6539;&#x591A;&#x6761;&#x76F8;&#x540C;&#x7684;&#x6587;&#x6863;&#xFF0C;&#x5219;&#x9700;&#x8981;&#x8BBE;&#x7F6E; multi &#x53C2;&#x6570;&#x4E3A; true&#x3002;</p>
<pre><code class="lang-sql">&gt;db.col.update({&apos;title&apos;:&apos;MongoDB &#x6559;&#x7A0B;&apos;},{$set:{&apos;title&apos;:&apos;MongoDB&apos;}},{multi:true})
</code></pre>
<hr>
<h2 id="save-&#x65B9;&#x6CD5;">save() &#x65B9;&#x6CD5;</h2>
<p>save() &#x65B9;&#x6CD5;&#x901A;&#x8FC7;&#x4F20;&#x5165;&#x7684;&#x6587;&#x6863;&#x6765;&#x66FF;&#x6362;&#x5DF2;&#x6709;&#x6587;&#x6863;&#x3002;&#x8BED;&#x6CD5;&#x683C;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-sql">db.collection.save(
   &lt;document&gt;,
   {
     writeConcern: &lt;document&gt;
   }
)
</code></pre>
<p>&#x53C2;&#x6570;&#x8BF4;&#x660E;&#xFF1A;</p>
<ul>
<li><strong>document</strong> : &#x6587;&#x6863;&#x6570;&#x636E;&#x3002;</li>
<li><strong>writeConcern</strong> :&#x53EF;&#x9009;&#xFF0C;&#x629B;&#x51FA;&#x5F02;&#x5E38;&#x7684;&#x7EA7;&#x522B;&#x3002;</li>
</ul>
<h2 id="&#x5B9E;&#x4F8B;">&#x5B9E;&#x4F8B;</h2>
<p>&#x4EE5;&#x4E0B;&#x5B9E;&#x4F8B;&#x4E2D;&#x6211;&#x4EEC;&#x66FF;&#x6362;&#x4E86; _id &#x4E3A; 56064f89ade2f21f36b03136 &#x7684;&#x6587;&#x6863;&#x6570;&#x636E;&#xFF1A;</p>
<pre><code class="lang-sql">&gt;db.col.save({
    &quot;_id&quot; : ObjectId(&quot;56064f89ade2f21f36b03136&quot;),
    &quot;title&quot; : &quot;MongoDB&quot;,
    &quot;description&quot; : &quot;MongoDB &#x662F;&#x4E00;&#x4E2A; Nosql &#x6570;&#x636E;&#x5E93;&quot;,
    &quot;by&quot; : &quot;Runoob&quot;,
    &quot;url&quot; : &quot;http://www.runoob.com&quot;,
    &quot;tags&quot; : [
            &quot;mongodb&quot;,
            &quot;NoSQL&quot;
    ],
    &quot;likes&quot; : 110
})
</code></pre>
<p>&#x66FF;&#x6362;&#x6210;&#x529F;&#x540E;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x901A;&#x8FC7; find() &#x547D;&#x4EE4;&#x6765;&#x67E5;&#x770B;&#x66FF;&#x6362;&#x540E;&#x7684;&#x6570;&#x636E;</p>
<pre><code class="lang-sql">&gt;db.col.find().pretty()
{
    &quot;_id&quot; : ObjectId(&quot;56064f89ade2f21f36b03136&quot;),
    &quot;title&quot; : &quot;MongoDB&quot;,
    &quot;description&quot; : &quot;MongoDB &#x662F;&#x4E00;&#x4E2A; Nosql &#x6570;&#x636E;&#x5E93;&quot;,
    &quot;by&quot; : &quot;Runoob&quot;,
    &quot;url&quot; : &quot;http://www.runoob.com&quot;,
    &quot;tags&quot; : [
            &quot;mongodb&quot;,
            &quot;NoSQL&quot;
    ],
    &quot;likes&quot; : 110
}
&gt;
</code></pre>
<h2 id="&#x66F4;&#x591A;&#x5B9E;&#x4F8B;">&#x66F4;&#x591A;&#x5B9E;&#x4F8B;</h2>
<pre><code class="lang-sql">&#x53EA;&#x66F4;&#x65B0;&#x627E;&#x5230;&#x7B26;&#x5408;&#x6761;&#x4EF6;&#x7684;&#x7B2C;&#x4E00;&#x6761;
db.col.update( { &quot;count&quot; : { $gt : 1 } } , { $set : { &quot;sex&quot; : &quot;&#x5973;&quot;} } );


&#x5168;&#x90E8;&#x66F4;&#x65B0;&#xFF1A; &#x627E;&#x5230;&#x7B26;&#x5408;&#x6761;&#x4EF6;&#x7684;&#x5168;&#x90E8;&#x66F4;&#x65B0;,&#x5982;&#x679C;&#x6CA1;&#x6709;&#x7B26;&#x5408;&#x6761;&#x4EF6;&#x7684;&#x4E0D;&#x4F1A;&#x8FDB;&#x884C;&#x6DFB;&#x52A0;&#x6570;&#x636E;  &#x5982;&#x679C;&#x7B26;&#x5408;&#x6761;&#x4EF6;&#x7684;&#x6570;&#x636E;&#x4E2D;&#x6CA1;&#x6709;&#x5BF9;&#x5E94;&#x5B57;&#x6BB5;,&#x5C31;&#x4F1A;&#x6DFB;&#x52A0;&#x5B57;&#x6BB5;&#x548C;&#x6570;&#x636E;
db.col.update( { &quot;count&quot; : { $gt : 20 } } , { $set : { &quot;sex&quot; : &quot;OK&quot;,&quot;love&quot;:&quot;iloveyou&quot;} },false,true );


&#x5982;&#x679C;&#x80FD;&#x627E;&#x5230;&#x7B2C;&#x4E00;&#x4E2A;&#x7B26;&#x5408;&#x6761;&#x4EF6;&#x7684;&#x6570;&#x636E; &#x5C31;&#x8FDB;&#x884C;&#x4FEE;&#x6539;  &#x5982;&#x679C;&#x6CA1;&#x6709;&#x627E;&#x5230;&#x7B26;&#x5408;&#x6761;&#x4EF6;&#x7684;&#x5C31;&#x662F;&#x8FDB;&#x884C;&#x6DFB;&#x52A0;&#x6570;&#x636E;
db.col.update( { &quot;count&quot; : { $lt : 2 } } , { $set : { &quot;test5&quot; : &quot;OK&quot;} },true,false );


&#x627E;&#x7B26;&#x5408;&#x6761;&#x4EF6;&#x7684;&#x6240;&#x6709;&#x6570;&#x636E;&#x8FDB;&#x884C;&#x4FEE;&#x6539;.,&#x5982;&#x679C;&#x6CA1;&#x6709;&#x7B26;&#x5408;&#x6761;&#x4EF6;&#x7684;&#x6570;&#x636E; &#x5C31;&#x8FDB;&#x884C;&#x6DFB;&#x52A0;
db.col.update( { &quot;count&quot; : { $lt : 5 } } , { $set : { &quot;test6&quot; : &quot;OK&quot;} },true,true );


&#x5168;&#x90E8;&#x66F4;&#x65B0;&#xFF1A;
db.col.update( { &quot;count&quot; : { $gt : 15 } } , { $inc : { &quot;count&quot; : 1} },false,true );
&#x53EA;&#x66F4;&#x65B0;&#x7B2C;&#x4E00;&#x6761;&#x8BB0;&#x5F55;&#xFF1A;
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../../home/part2/3.MongoDBCreate.html" class="navigation navigation-prev " aria-label="Previous page: MongoDB Create"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../../home/part2/5.MongoDBDelete.html" class="navigation navigation-next " aria-label="Next page: MongoDB Delete"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../../gitbook/app.js"></script>

    
    <script src="../../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
